
import backend.app.app.models.db_sys_models as md
from backend.app.app.db.session_sys import session

# 添加新用户注册信息
def add_user_regis(update_date, register_date, check_date, user_id, user_pwd, remark,
                       user_name, user_role, belong_depart,
                       job_name, user_truname, user_mobilenumber,
                       user_email, legalperson_id,
                       isqualified, check_name):
    new_user = md.TbUser(User_ID=user_id,
                         User_Name=user_name,
                         User_PWD=user_pwd,
                         User_Role=user_role,
                         Belong_Depart=belong_depart,
                         Job_Name=job_name,
                         User_TruName=user_truname,
                         User_MobileNumber=user_mobilenumber,
                         User_Email=user_email,
                         Register_Date=register_date,
                         IsQualified=isqualified,
                         Check_Date=check_date,
                         Check_Name=check_name,
                         Update_Date=update_date,
                         Remark=remark,
                         LegalPerson_ID=legalperson_id,
                         Online=0,
                         )
    try:
        session.add(new_user)
        session.commit()
        print('新建用户成功')
        session.close()
        return True
    except Exception as e:
        print(e,e.__traceback__.tb_lineno)
        return False



# 添加新管理员
def add_newadmin(update_date, register_date, check_date, user_id, user_pwd, remark,
                user_name, user_role, belong_depart,
                job_name, user_truname, user_mobilenumber,
                user_email, legalperson_id,
                isqualified, check_name):
    new_admin = md.TbAdministrator(User_ID=user_id,
                                   User_Name=user_name,
                                   User_PWD=user_pwd,
                                   User_Role=user_role,
                                   Belong_Depart=belong_depart,
                                   Job_Name=job_name,
                                   User_TruName=user_truname,
                                   User_MobileNumber=user_mobilenumber,
                                   User_Email=user_email,
                                   Register_Date=register_date,
                                   IsQualified=isqualified,
                                   Check_Date=check_date,
                                   Check_Name=check_name,
                                   Update_Date=update_date,
                                   Remark=remark,
                                   LegalPerson_ID=legalperson_id)
    try:
        session.add(new_admin)
        session.commit()
        print('新建管理员成功')
        session.close()
        return True
    except Exception as e:
        print('新建管理员失败',e,e.__traceback__.tb_lineno)
        return  False


# 存储管理员管理框架界面表格数据,并将相同管理员上一条数据删除
def save_frame_table_data(user_id,data):
    try:
        session.query(md.TbAdminFrameTable).filter(md.TbAdminFrameTable.User_id == user_id).delete()
        table_data = md.TbAdminFrameTable(User_id=user_id,
                                          Data=data)
        session.add(table_data)
        session.commit()
        print('添加数据成功')
        session.close()
        return True
    except:
        print('添加数据失败')
        return False


# 从管理员管理框架界面表中返回数据
def reurn_frame_table_data(user_id):
    try:
        data = session.query(md.TbAdminFrameTable.Data).filter(md.TbAdminFrameTable.User_id == user_id).first()
        print(data)
        return data
    except:
        return False


# 存储管理员界面用户信息表格数据,并将相同管理员上一条数据删除
def save_info_table_data(user_id, data):
    try:
        session.query(md.TbAdminInfoTable).filter(md.TbAdminInfoTable.User_id == user_id).delete()
        table_data = md.TbAdminInfoTable(User_id=user_id,
                                         Data=data)
        session.add(table_data)
        session.commit()
        print('添加数据成功')
        session.close()
        return True
    except:
        print('添加数据失败')
        return False

# 从管理员界面用户信息表中返回数据
def reurn_info_table_data(user_id):
    try:
        data = session.query(md.TbAdminInfoTable.Data).filter(md.TbAdminInfoTable.User_id == user_id).first()
        print(data)
        return data
    except:
        return False


if __name__ == '__main__':
    reurn_info_table_data(user_id=2
                            )
#     add_newadmin(user_id=5, update_date='2021-04-23', register_date='2021-04-23', check_date='2021-04-23')
